package com.itheima.utils;
import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Map;

/**
 * @Description: 阿里大于短信发送工具类
 * @Date :2019-10-24 14:17
 * @Author : yuan_he_song
 */
@Component
public class AliSendUtils {

    private static  String AccessKeyID;
    private static  String AccessKeySecret;
    public  static  String TemplateCode;
    private static  String SignName;

    @Value("${AccessKeyID}")
    private  String proxyAccessKeyID;
    @Value("${AccessKeySecret}")
    private  String proxyAccessKeySecret;
    @Value("${SignName}")
    private  String proxySignName;
    @Value("${TemplateCode}")
    private String proxyTemplateCode;

    /**
     * @param telephone : 根据手机号发送
     * @param paramers : 要发送的验证码
     */
    public static void send(String telephone, String paramers){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", AccessKeyID, AccessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        /** 要发送的手机号码 */
        request.putQueryParameter("PhoneNumbers", telephone);
        /** 短信签名 */
        request.putQueryParameter("SignName", SignName);
        /** 短信模板 */
        request.putQueryParameter("TemplateCode", TemplateCode);

        Map<String,String> map = Maps.newHashMap();
        map.put("code",paramers);
        request.putQueryParameter("TemplateParam", JSON.toJSONString(map));
//        request.putQueryParameter("TemplateParam", "{\"code\":"+ paramers +"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.err.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    public static void setAccessKeyID(String accessKeyID) {
        AccessKeyID = accessKeyID;
    }

    public static void setAccessKeySecret(String accessKeySecret) {
        AccessKeySecret = accessKeySecret;
    }

    public static void setTemplateCode(String templateCode) {
        TemplateCode = templateCode;
    }

    public static void setSignName(String signName) {
        SignName = signName;
    }

    @PostConstruct
    public void setProperties(){
        setAccessKeyID(this.proxyAccessKeyID);
        setAccessKeySecret(this.proxyAccessKeySecret);
        setSignName(this.proxySignName);
        setTemplateCode(this.proxyTemplateCode);
    }
}
